function [K,Weights,name]=puttestshere2(F,width,height,numparams,corrs)
%close all
showplots=0;
Weights=[];
name='mean shift';
K=[width 0 width/2; 0 width height/2; 0 0 1];
numsl=150;
usedFs=1:size(F,2);




params(1) = struct('name', 'focal','ymeasure','foc1' ,'xmeasure','foc2', 'yKrow', 1, 'yKcol', 1, 'xKrow',2, 'xKcol', 2);
params(2) = struct('name', 'oc','ymeasure','xc' ,'xmeasure','yc', 'yKrow', 1, 'yKcol', 3, 'xKrow',2, 'xKcol', 3);


%for z=1:2
for p=1:size(params,2)
    
    allpoints=[];
    
    for i=1:size(usedFs,2)
        [temparay,xarr]= provideFamilySolution_matrixnorm(F{1,usedFs(1,i)},width,height,2,numsl,K,params(p).ymeasure);
        if(size(F{1,usedFs(1,i)},1)~=0)
            allpoints=[ allpoints ; xarr temparay ones(size(xarr,1),1)*i];
        end
    end
    

    u=findbw(allpoints(:,2));
    
    
    [clustCent,~,clustMembsCell]  = MeanShiftCluster(allpoints(:,1:2)',u);
    %[clustMembsCell,clustCent]= myownkmeans(allpoints(:,1:2));
    
    
    indbest=findbestcluster(clustMembsCell,allpoints,clustCent);
    centerbest=clustCent(:,indbest);
    
    if(showplots==1)
        plotclustersdata(allpoints, clustMembsCell,clustCent,indbest,params(p).ymeasure, params(p).xmeasure)
    end
    
    
    K(params(p).xKrow,params(p).xKcol)=centerbest(1,1);
    K(params(p).yKrow,params(p).yKcol)=centerbest(2,1);
    %   display([params(p).xmeasure ' was ' num2str(K(params(p).xKrow,params(p).xKcol))]);
    %   display([params(p).ymeasure ' was ' num2str(K(params(p).yKrow,params(p).yKcol))]);
    
end
%end

end

function [u]=findbw(data)
N=size(data,1);
med = median(data);
sig = median(abs(data-med)) / 0.6745;
u = sig * (4/(3*N))^(1/5);
end

function []=plotclustersdata(x, clustMembsCell,clustCent,indbest,label1, label2)
cVec = 'bgrcmykbgrcmykbgrcmykbgrcmyk';%, cVec = [cVec cVec];
numClust = length(clustMembsCell);

usedf = unique(x(:,3));
numfs=length(usedf);

figure
subplot(1,3,1),hold on
for k = 1:numfs
    scatter(x(x(:,3)==usedf(k,1),1),x(x(:,3)==usedf(k,1),2),[cVec(1+mod(k,length(cVec))) '.'])
end
title(['generated solutions']);
xlabel(label2);
ylabel(label1);

subplot(1,3,2),hold on
for k = 1:numClust
    myMembers = clustMembsCell{k};
    myClustCen = clustCent(:,k);
    scatter(x(myMembers,1),x(myMembers,2),[cVec(1+mod(k,length(cVec))) '.'])
    plot(myClustCen(1),myClustCen(2),'o','MarkerEdgeColor','k','MarkerFaceColor',cVec(1+mod(k,length(cVec))), 'MarkerSize',10)
end
xlabel(label2);
ylabel(label1);
title(['segmented clusters '  label1 ' versus '  label2]);

subplot(1,3,3),hold on
for k = 1:numClust
    if(numClust~= indbest)
        myMembers = clustMembsCell{k};
        scatter(x(myMembers,1),x(myMembers,2),['b.'])
    end
end
myMembers = clustMembsCell{indbest};
scatter(x(myMembers,1),x(myMembers,2),['r.']);
plot(clustCent(1,indbest),clustCent(2,indbest),'o','MarkerEdgeColor','k','MarkerFaceColor','r', 'MarkerSize',10);
title(['winning cluster '  label1 ' versus '  label2]);
xlabel(label2);
ylabel(label1);
hold off
end

function [clustMembsCell,clustCent]=myownkmeans(data)
numclusters=20;

[idx,clustCentraw,sumd] = kmeans(data,numclusters,'Replicates',5,'emptyaction','drop','display','off');
clustCent=clustCentraw';
numclusts=size(clustCentraw,1);
clustMembsCell=cell(numclusts,1);
ptsize=size(idx,1);

for j=1:ptsize
    cluster=idx(j,1);
    clustMembsCell{cluster,1}=[clustMembsCell{cluster,1} j];
end



end
function [bestind]=findbestcluster(cell2clusts,data,clustCent)
usedf = unique(data(:,3));
nummodels=length(usedf);
maxscore=-100;
numClust = length(cell2clusts);


% origincluster=-1;
% smallestdist=1000000;
% for k = 1:numClust
%     dist=sqrt(clustCent(1,k)^2+clustCent(2,k)^2);
%     if(dist<smallestdist)
%        smallestdist=dist;
%        origincluster=k;
%     end
% end
% bestind=-1;
% numClust = length(cell2clusts);
% for k = 1:numClust
%     if(size(cell2clusts{k},2)>maxscore)
%         maxscore=size(cell2clusts{k},2);
%         bestind=k;
%     end
% end



for k = 1:numClust
%     if(k==origincluster)
%        continue
%     end
    scores=zeros(data(end,3),1);
    pointz=cell2clusts{k};
    sz=size(pointz,2);
    
    for i=1:sz
        curind=data(pointz(1,i),3);
        if(scores(curind,1)==0)
            scores(curind,1)=1;
        end
    end
    
    
    if(sum(scores)>maxscore)
        maxscore=sum(scores);
        bestind=k;
    end
end

end
